﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using cart.service;
using cart.model;
using cart.utils;

namespace cart.web
{
    public partial class shopCart : System.Web.UI.Page
    {
        private double sum = 0;//求总的和。

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                int productID = System.Convert.ToInt32(this.Request.QueryString["productID"]);
                if (0 != productID)
                {

                    productService ps = new productService();

                    ProductModel pm = ps.getProdcutByID(productID);

                    if (null != pm)
                    {
                        cartModel cm = new cartModel();
                        cm.userName = "ousinka";
                        cm.productID = pm.productID;
                        cm.productNumber = 1;
                        cm.addTime = DateTime.Now;
                        cartService cs = new cartService();
                        cs.addCart(cm);
                        //if (cs.addCart(cm))
                        //{
                        //    this.Response.Write("<script type=text/javascript>alert('添加" + pm.name + "成功！')</script> ");
                        //}
                        //else
                        //{ this.Response.Write("<script type=text/javascript>alert('添加" + pm.name + "失败！')</script> "); }
                    }
                }

                dataBinder();
            }
        }

        public void dataBinder()
        {
            //ArrayList cartLists = new ArrayList();
            //ArrayList productLists = new ArrayList();
            //cartService cs = new cartService();

            //productService ps = new productService();


            //cartLists = cs.getCartByUserName("ousinka");
            //foreach (cartModel cm in cartLists)
            //{
            //    ProductModel pm = new ProductModel();
            //    pm = ps.getProdcutByID(cm.productID);
            //    productLists.Add(pm);                
            //}
            //this.gvCart.DataSource =productLists;


            ArrayList cart = getCartData();

            if(0== cart.Count)
            {
                this.Response.Write("购物车为空！<br>");
                this.Response.Write("<a href=Default.aspx ><<购物</a>");
                //this.Response.Redirect("Default.aspx");
            }

            this.gvCart.DataSource = cart;
            this.gvCart.DataBind();
        }

        private ArrayList getCartData()
        {
            ArrayList cartLists = new ArrayList();
            ArrayList cart = new ArrayList();
            cartService cs = new cartService();
            cartLists = cs.getCartByUserNameOrProductID("ousinka");
            cart = cs.getCart(cartLists);
            sum = cs.getCartTotle(cart);
            return cart;
        }

        protected void gvCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productID = Convert.ToInt32(gvCart.Rows[e.RowIndex].Cells[0].Text.ToString().Trim());
            //this.Response.Write("删除："+productID);
            cartModel cm = new cartModel();
            cm.userName = "ousinka";
            cm.productID = productID;
            cartService cs = new cartService();
            cs.deleteCartItem(cm);
            dataBinder();
        }

        protected void gvCart_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvCart.EditIndex = e.NewEditIndex;
            dataBinder();
        }

        protected void gvCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvCart.EditIndex = -1;
            dataBinder();
        }

        protected void gvCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int productID = Convert.ToInt32(gvCart.Rows[e.RowIndex].Cells[0].Text.ToString().Trim());
            try
            {
                int productNumber = Convert.ToInt32(((TextBox)(gvCart.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim());

                cartModel cm = new cartModel();
                cartService cs = new cartService();

                cm.productID = productID;
                cm.userName = "ousinka";
                cm.productNumber = productNumber;
                cs.updateCartItem(cm);
            }
            catch
            {
                this.Response.Write("<script>alert('你骗人，这个数量怎么买啊？')</script>");
            }
            //this.Response.Write("将" + productID + "的数量更新成为：" + productNumber);


            gvCart.EditIndex = -1;
            dataBinder();
        }

        //让gvCart的footer显示总价格
        protected void gvCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowIndex >= 0)
            //{
            //    sum += Convert.ToDouble(e.Row.Cells[4].Text);
            //}
            //else
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[2].Text = "<a href=Default.aspx style='color:White; text-decoration: none'><<继续</a>";
                e.Row.Cells[3].Text = "总价格为：";
                e.Row.Cells[4].Text = sum.ToString();
                e.Row.Cells[6].Text = "<a href=\"javascript:__doPostBack('lbAddCart','')\" style='color:White; text-decoration: none'>结算>></a>";
            }
        }


        protected void lbAddCart_Click(object sender, EventArgs e)
        {
            //this.Response.Write("haha");
            orderServiec os = new orderServiec();
            ArrayList cart = getCartData();
            int orderID = os.saveOrder(cart, "ousinka");
            cartService cs = new cartService();
            cs.clearCart("ousinka");
            this.Response.Redirect("checkout.aspx?orderID=" + orderID);
        }

    }
}
